package com.iqiyi.pps.epg.core.utils;

import com.iqiyi.kiwi.utils.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lemuel on 2018/03/02.
 */
public final class ApiFeedUtil {
    private static Logger logger = LoggerFactory.getLogger(ApiFeedUtil.class);

    private ApiFeedUtil() {
    }

    private static final String ADD_URL = Configuration.getString("api.feed.add.url");
    private static final String DEL_URL = Configuration.getString("api.feed.del.url");
    private static final String ADD_TOKEN_KEY = Configuration.getString("api.feed.add.token.key");
    private static final String DEL_TOKEN_KEY = Configuration.getString("api.feed.del.token.key");
    public static final long DEFAULT_IQIYI_UID = 1439698922;

    public static List<String> addFeed(long programQipuId, List<String> circleList, long uid) {
        Map<String, String> params = new HashMap<>();
        params.put("extendType", "5");
        params.put("entityId", programQipuId + "");
        params.put("token", MD5Util.getMD5String(ADD_TOKEN_KEY + programQipuId));
        params.put("userType", "20");
        params.put("sourceType", "103");
        if (uid > 0) {
            params.put("uid", uid + "");
        } else {
            params.put("uid", DEFAULT_IQIYI_UID + "");
        }
        StringBuffer stringBuffer = new StringBuffer();
        int count = 0;
        List<String> resultList = new ArrayList<String>();
        if (circleList.size() > 0) {
            for (String single : circleList) {
                stringBuffer.append(single + ",");
                count++;
            }
            if (count > 0) {
                stringBuffer.deleteCharAt(stringBuffer.length() - 1);
                params.put("wallId", stringBuffer.toString());
                resultList.add(ApiHttpRequestUtil.doGet(ADD_URL, null, params));
            }
        } else {
            resultList.add(ApiHttpRequestUtil.doGet(ADD_URL, null, params));
        }
        return resultList;
    }

    public static String delFeed(long programQipuId) {
        Map<String, String> params = new HashMap<>();
        params.put("tvId", "" + programQipuId);
        params.put("token", MD5Util.getMD5String(DEL_TOKEN_KEY + programQipuId));
        return ApiHttpRequestUtil.doGet(DEL_URL, null, params);
    }

}
